import { cumulate, arrange, policyCompute } from "../../utils";
import pc45 from "@/views/FlowStatus/utils/PolicyCompute_v4.5.js";

import dayjs from "dayjs";

const barOpacity = 0.9;

const generateArrangedData = ({
  rawData,
  tags,
  begTime,
  dataInterval,
  dataStep,
}) => {
  if (!rawData || rawData.length == 0) return [];
  let cumulatedRawData = cumulate(rawData, dataInterval, dataStep);

  let arrangedData = pc45(
    arrange(cumulatedRawData, tags, begTime, dataInterval, [
      "airport",
      "type",
      "point",
    ]),
    dataInterval,
    true
  );

  if (dataInterval == 2) {
    return arrangedData.slice(5, arrangedData.length);
  } else if (dataInterval == 1) {
    return arrangedData.slice(14, arrangedData.length);
  } else {
    return arrangedData;
  }
};

const generateSeriesData = ({ res, flowLimits, dataStep }) => {
  if (!res || res.length == 0) {
    return null;
  }
  let content = {
    data: {
      sector: res.map((x) => {
        let val = x.sectorRequireCn;
        return {
          value: val,
          label: {
            backgroundColor:
              val > 1
                ? "#56A902"
                : val == 1
                ? "#56A902"
                : val == 0.5
                ? "#2b90d9"
                : "#5e5e5f",
          },
        };
      }),
      ZUYB: {
        ALL: res.map((x) => {
          let v = x.airport.ZUYB.total;
          return {
            value: v,
            itemStyle: {
              opacity: v == 0 ? 0 : barOpacity,
            },
          };
        }),
        DEP: res.map((x) => {
          let v = x.airport.ZUYB.type.DEP.total;
          return {
            value: v,
            itemStyle: {
              opacity: v == 0 ? 0 : barOpacity,
            },
          };
        }),
        ARR: res.map((x) => {
          let v = x.airport.ZUYB.type.ARR.total;
          return {
            value: v,
            itemStyle: {
              opacity: v == 0 ? 0 : barOpacity,
            },
          };
        }),
      },
      ZULZ: {
        ALL: res.map((x) => {
          let v = x.airport.ZULZ.total;
          return {
            value: v,
            itemStyle: {
              opacity: v == 0 ? 0 : barOpacity,
            },
          };
        }),
        DEP: res.map((x) => {
          let v = x.airport.ZULZ.type.DEP.total;
          return {
            value: v,
            itemStyle: {
              opacity: v == 0 ? 0 : barOpacity,
            },
          };
        }),
        ARR: res.map((x) => {
          let v = x.airport.ZULZ.type.ARR.total;
          return {
            value: v,
            itemStyle: {
              opacity: v == 0 ? 0 : barOpacity,
            },
          };
        }),
      },
      APP: {
        ALL: res.map((x) => {
          let v = x.airport.ZUYB.total + x.airport.ZULZ.total;
          return {
            value: v,
            itemStyle: {
              opacity: v == 0 ? 0 : barOpacity,
            },
          };
        }),
        DEP: res.map((x) => {
          let v = x.airport.ZUYB.type.DEP.total + x.airport.ZULZ.type.DEP.total;
          return {
            value: v,
            itemStyle: {
              opacity: v == 0 ? 0 : barOpacity,
            },
          };
        }),
        ARR: res.map((x) => {
          let v = x.airport.ZUYB.type.ARR.total + x.airport.ZULZ.type.ARR.total;
          return {
            value: v,
            itemStyle: {
              opacity: v == 0 ? 0 : barOpacity,
            },
          };
        }),
      },
      OTHER: res.map((x) => {
        let v =
          x.airport.ZUYB.type.OTHER.total + x.airport.ZULZ.type.OTHER.total;
        return {
          value: v,
          itemStyle: {
            opacity: v == 0 ? 0 : barOpacity,
          },
        };
      }),
    },
    moment: res.map((x) => x.moment),
    timestamp: res.map((x) => x.timestamp),
    scaleY: res.map((x) => {
      let diff = dayjs().diff(x.timestamp, "minute");
      let passed = diff < 0 ? 1 : diff > dataStep * 15 ? -1 : 0;
      let v = dayjs(x.timestamp).format("HHmm");
      return {
        value: `${v} +${dataStep * 15}`,
        textStyle: {
          color:
            passed === 1 ? "#409EFF" : passed === 0 ? "#67C23A" : "#909399",
          fontWeight: passed === 0 ? "bold" : "normal",
          opacity: passed === 0 ? 1 : 0.9,
          overflow: "break",
        },
      };
    }),
    warnings: [],
  };
  if (flowLimits) {
    // console.log(flowLimits);
    let warningLabelConfig = {
      color: "#A00",
      fontSize: "20px",
      textBorderColor: "#FFF",
      /*
      commonStore.darkMode
        ? "rgba(255,255,255,0.9)"
        : "rgba(0,0,0,0.9)",
        F*/
      textBorderWidth: 2,
    };
    for (let i = 0; i < content.moment.length; i++) {
      if (
        flowLimits.ZUYB &&
        isOverflow(i, content.data.ZUYB.ALL, dataStep, flowLimits.ZUYB.ALL)
      ) {
        content.data.ZUYB.ALL[i].label = warningLabelConfig;
        // content.scaleY[i].textStyle.color = "#A00";
        content.warnings.push({
          t: content.scaleY[i].value,
          m: "ZUYB总量超量",
        });
      }
      if (
        flowLimits.ZUYB &&
        isOverflow(i, content.data.ZUYB.ARR, dataStep, flowLimits.ZUYB.ARR)
      ) {
        content.data.ZUYB.ARR[i].label = warningLabelConfig;
        // content.scaleY[i].textStyle.color = "#A00";
        content.warnings.push({
          t: content.scaleY[i].value,
          m: "ZUYB进港超量",
        });
      }
      if (
        flowLimits.ZULZ &&
        isOverflow(i, content.data.ZULZ.ALL, dataStep, flowLimits.ZULZ.ALL)
      ) {
        content.data.ZULZ.ALL[i].label = warningLabelConfig;
        // content.scaleY[i].textStyle.color = "#A00";
        content.warnings.push({
          t: content.scaleY[i].value,
          m: "ZULZ总量超量",
        });
      }
      if (
        flowLimits.ZULZ &&
        isOverflow(i, content.data.ZULZ.ARR, dataStep, flowLimits.ZULZ.ARR)
      ) {
        content.data.ZULZ.ARR[i].label = warningLabelConfig;
        // content.scaleY[i].textStyle.color = "#A00";
        content.warnings.push({
          t: content.scaleY[i].value,
          m: "ZULZ进港超量",
        });
      }
    }
  }

  return content;
};

const isOverflow = (i, res, dataStep, limit) => {
  // return true;
  if (limit == 0) {
    return false;
  } else {
    let value = res[i].value;
    let value2 = res[i].value;
    if (dataStep == 1) {
      if (i < res.length - 3) {
        value += res[i + 1].value + res[i + 2].value + res[i + 3].value;
      }
      if (i > 2) {
        value2 += res[i - 1].value + res[i - 2].value + res[i - 3].value;
      }
    } else if (dataStep == 2) {
      if (i < res.length - 1) {
        value += res[i + 1].value;
      }
      if (i > 1) {
        value2 += res[i - 1].value;
      }
    }
    let result = null;
    if (value > limit || value2 > limit) {
      result = true;
    } else {
      result = false;
    }
    // console.log("isOverflow", "i:", i, "value", value, "limit", limit, result);
    return result;
  }
};

export default function ({
  rawData,
  tags,
  begTime,
  dataInterval,
  dataStep,
  flowLimits,
}) {
  const arrangedData = generateArrangedData({
    rawData,
    tags,
    begTime,
    dataInterval,
    dataStep,
  });
  const seriesData = generateSeriesData({
    res: arrangedData,
    flowLimits,
    dataStep,
  });
  seriesData["rawData"] = rawData;
  seriesData["arrangedData"] = arrangedData;
  return seriesData;
}
